package cn.itcast.service.impl;

import cn.itcast.pojo.CheckGroup;
import cn.itcast.pojo.CheckItem;
import cn.itcast.pojo.Setmeal;
import cn.itcast.service.CheckGroupService;
import cn.itcast.service.CheckItemService;
import cn.itcast.service.MobileSetmealCheckgroupService;
import cn.itcast.service.SetmealService;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;
@Service
public class MobileSetmealCheckgroupServiceImpl implements MobileSetmealCheckgroupService {

    @Autowired//检查套餐
    private SetmealService setmealService;

    @Autowired//检查组
    private CheckGroupService checkGroupService;

    @Autowired//检查项
    private CheckItemService checkItemService;

    /**
     * 根据检查套餐id查询到该套餐的详细信息
     * @param id
     * @return
     */
    @Override
    public Setmeal querySetmeal(Integer id) {
        System.out.println(id);
        //查询出该套餐的所有检查组id
        Integer[] groupIds = this.setmealService.selectedCheckGroupIds(id);
        //封装数据填充
        Setmeal setmeal = this.setmealService.findById(id);
        List<CheckGroup> checkGroupList = new ArrayList<>();
        for (Integer groupId : groupIds) {//遍历检测组
            List<CheckItem> checkItemList = new ArrayList<>();
            CheckGroup checkGroup = this.checkGroupService.findById(groupId);//根据检查组id查询检测组详细信息
            Integer[] integers = this.checkGroupService.selectedCheckItemsId(groupId);
            for (Integer integer : integers) {//遍历检查项
                CheckItem checkItem = this.checkItemService.findById(integer);
                checkItemList.add(checkItem);//将该检查组的检查项封装
                checkGroup.setCheckItems(checkItemList);
            }
            checkGroupList.add(checkGroup);//将该检查套餐的检测组封装
            setmeal.setCheckGroups(checkGroupList);
        }

        return setmeal;
    }
}
